package ru.usu.gv.utils.exports;

import ru.usu.gv.core.GraphCanvas;
import ru.usu.gv.uicomponents.UIEdge;
import ru.usu.gv.uicomponents.UIGraph;
import ru.usu.gv.uicomponents.UIVertex;

import javax.media.j3d.Transform3D;
import javax.vecmath.Point2d;
import javax.vecmath.Point3d;

import java.awt.Color;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;

public class SVGExporter
{

	private static final double SCALE = 500;

	public void export(UIGraph graph, PrintStream stream)
	{
		Map<UIVertex, Point2d> coordinates = buildCoordinates(graph);

		outputCoords(coordinates);

		stream.println("<?xml version='1.0' encoding='utf-8' standalone='yes'?>");
		stream.println("<svg version = '1.1' baseProfile='full'");
		stream.println("	xmlns = 'http://www.w3.org/2000/svg'");
		stream.println("	xmlns:xlink = 'http://www.w3.org/1999/xlink'");
		stream.println("	xmlns:ev = 'http://www.w3.org/2001/xml-events'");
		stream.println("	height = '100%'  width = '100%'>\n");

		for (UIEdge e : graph.getEdges())
		{
			UIVertex v1 = e.getV1();
			UIVertex v2 = e.getV2();
			double x1 = coordinates.get(v1).x * SCALE;
			double y1 = coordinates.get(v1).y * SCALE;
			double x2 = coordinates.get(v2).x * SCALE;
			double y2 = coordinates.get(v2).y * SCALE;
			Color c = e.getViewItem().getColor();

			double len = coordinates.get(v1).distance(coordinates.get(v2));

			double a = 0.7 - len;
			a = Math.min(a, 1.0);
			a = Math.max(a, 0.15);
			String aa = String.valueOf(a).replaceAll(",", ".");

			String s = String.format("	<line x1='%.3f' y1='%.3f' x2='%.3f' y2='%.3f' style='stroke:%s;stroke-width:1;fill:none;stroke-opacity:%s'/>", x1, y1, x2, y2, rgb(c), aa);
			stream.println(s);
		}

		stream.println();

		for (UIVertex v : graph.getVertexes())
		{
			double x = coordinates.get(v).x * SCALE;
			double y = coordinates.get(v).y * SCALE;
			double r = v.getViewItem().getSize() * SCALE;
			Color c = v.getViewItem().getColor();
			String s;

			if (v.getVertexType().getId().equals("sphere"))
				s = String.format("	<circle cx='%.3f' cy='%.3f' r='%.3f' fill='%s'/>", x, y, r, rgb(c));
			else
				s = String.format("	<rect x='%.3f' y='%.3f' width='%.3f' height='%.3f' fill='%s'/>", x - r, y - r, r * 2, r * 2, rgb(c));

			stream.println(s);
		}

		stream.println("</svg>");
	}

	private void outputCoords(Map<UIVertex, Point2d> coordinates)
	{
		for (UIVertex v : coordinates.keySet())
		{
			Point2d p = coordinates.get(v);
			System.out.println(p.x + " " + p.y + " " + v.getLabel());
		}
	}

	private Map<UIVertex, Point2d> buildCoordinates(UIGraph graph)
	{
		Map<UIVertex, Point2d> res = new HashMap();

		Transform3D xyTransform = new Transform3D();
		GraphCanvas.getInstance().getTransformGroup().getTransform(xyTransform);

		for (UIVertex vertex : graph.getVertexes())
		{
			Point3d p = vertex.getPosition();
			Point3d pNew = new Point3d();
			xyTransform.transform(p, pNew);
			res.put(vertex, new Point2d(pNew.x, pNew.y));
		}

		return res;
	}

	private String rgb(Color color)
	{
		int r = color.getRed();
		int g = color.getGreen();
		int b = color.getBlue();
		return String.format("rgb(%d,%d,%d)", r, g, b);
	}
}
